package com.generator.storageManage.outboundManage.controller;

import com.generator.statics.outboundStatics.pojo.OutCountDetail;
import com.generator.storageManage.outboundManage.pojo.CommentDetial;
import com.generator.storageManage.outboundManage.pojo.OutCountDelivery;
import com.generator.storageManage.outboundManage.pojo.OutBoundObject;
import com.generator.storageManage.outboundManage.service.impl.OutBoundManageService;
import com.generator.storageManage.storeManage.pojo.OutCountObject;
import com.generator.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/OutBoundManage")
public class OutBoundManageAction {

    @Autowired
    private OutBoundManageService outBoundManageService;

    public OutBoundManageService getOutBoundManageService() {
        return outBoundManageService;
    }

    public void setOutBoundManageService(OutBoundManageService outBoundManageService) {
        this.outBoundManageService = outBoundManageService;
    }

    //取得分页后的出库表
    @ResponseBody
    @RequestMapping("/getOutBoundListAfterPage")
    public PageModel<OutBoundObject> getOutBoundListAfterPage(HttpServletRequest req){
        OutBoundObject outBoundObject = new OutBoundObject();
        PageModel<OutBoundObject> outCountObjectPageModel = new PageModel<>();
        String pagenum = req.getParameter("pagenum");
        if(pagenum!=null){
            Integer valueOf = Integer.valueOf(pagenum);
            outCountObjectPageModel.setPageNum(valueOf);
        }
        PageModel<OutBoundObject> outBoundpage = outBoundManageService.getOutBoundpage(outCountObjectPageModel, outBoundObject);
        return outBoundpage;
    }

    //去到改变出库状态
    @ResponseBody
    @RequestMapping("/tochangeOutBoundState")
    public String tochangeOutBoundState(HttpServletRequest req, HttpSession session){
        String outboundid = req.getParameter("outboundid");
        String action = req.getParameter("action");
        String stateid = req.getParameter("stateid");
        Integer obid = Integer.valueOf(outboundid);
        Integer steid = Integer.valueOf(stateid);
        Integer finalsteid=null;
        if(action.equals("tipOpen1")){
            switch (steid){
                case 1:finalsteid=2;break;
                case 2:finalsteid=3;break;
                case 3:finalsteid=4;break;
                case 4:finalsteid=5;break;
            }
        }else if(action.equals("tipOpen2")){
            finalsteid=4;
        }
        session.setAttribute("obid", obid);
        session.setAttribute("finalsteid", finalsteid);
        return "true";
    }

    //改变出库状态
    @ResponseBody
    @RequestMapping("/changeOutBoundState")
    public Integer changeOutBoundState(HttpSession session){
        Integer obid = (Integer)session.getAttribute("obid");
        Integer finalsteid = (Integer)session.getAttribute("finalsteid");

        return outBoundManageService.updateOutBoundState(obid, finalsteid);
    }

    //去到订单详情页并传入订单编号到session
    @ResponseBody
    @RequestMapping("/toCommentDeliveryView")
    public void toCommentDeliveryView(HttpServletRequest req,HttpSession session){
        String orderid = req.getParameter("orderid");
        session.setAttribute("orderid", orderid);
    }

    //取得订单页的详情（不包含商品信息）
    @ResponseBody
    @RequestMapping("/getDelivaeryDetail")
    public OutCountDelivery getDelivaeryDetail(HttpSession session){
        String orderid = (String)session.getAttribute("orderid");
        OutCountDelivery outCountDelivery = new OutCountDelivery();
        outCountDelivery.setOrderid(orderid);
        OutCountDelivery deliveryByOrderId = outBoundManageService.getDeliveryByOrderId(outCountDelivery);
        return deliveryByOrderId;
    }

    //取得订单页含商品信息
    @ResponseBody
    @RequestMapping("/getCommentDetialByOrderId")
    public List<CommentDetial> getCommentDetialByOrderId(HttpSession session){
        String orderid = (String)session.getAttribute("orderid");
        CommentDetial commentDetial = new CommentDetial();
        commentDetial.setOrderid(orderid);
        List<CommentDetial> commentDetialByOrderId = outBoundManageService.getCommentDetialByOrderId(commentDetial);
        return commentDetialByOrderId;
    }

    //搜索功能--完整
    @ResponseBody
    @RequestMapping("/checkStatic")
    public PageModel<OutBoundObject> checkStatic(HttpSession session, HttpServletRequest req, PageModel<OutCountObject> pageModel){
        String checkhouse = req.getParameter("checkhouse");
        String ordernumber = req.getParameter("ordernumber");
        String state = req.getParameter("state");
        Integer stateid=null;
        if(state!=""){
            stateid=Integer.valueOf(state);
        }
        OutBoundObject outBoundObject = new OutBoundObject();
        if(checkhouse==""&ordernumber!=""&state==""){
            outBoundObject.setOrderid(ordernumber);
        }else if(checkhouse==""&ordernumber!=""&state!=""){
            outBoundObject.setOrderid(ordernumber);
            outBoundObject.setStateid(stateid);
        }else if(checkhouse==""&ordernumber==""&state!=""){
            outBoundObject.setStateid(stateid);
        }else if(checkhouse!=""&ordernumber!=""&state==""){
            outBoundObject.setStoragename(checkhouse);
            outBoundObject.setOrderid(ordernumber);
        }else if(checkhouse!=""&ordernumber!=""&state!=""){
            outBoundObject.setStoragename(checkhouse);
            outBoundObject.setOrderid(ordernumber);
            outBoundObject.setStateid(stateid);
        }else if(checkhouse!=""&ordernumber==""&state!=""){
            outBoundObject.setStoragename(checkhouse);
            outBoundObject.setStateid(stateid);
        }else if(checkhouse!=""&ordernumber==""&state==""){
            outBoundObject.setStoragename(checkhouse);
        }
        PageModel<OutBoundObject> outCountObjectPageModel = new PageModel<>();
        String pagenum = req.getParameter("pagenum");
        if(pagenum!=null){
            Integer valueOf = Integer.valueOf(pagenum);
            outCountObjectPageModel.setPageNum(valueOf);
        }
        PageModel<OutBoundObject> totalOutCount = outBoundManageService.getOutBoundpage(outCountObjectPageModel, outBoundObject);
        return  totalOutCount;

    }

    //取得所有的仓库名称
    @ResponseBody
    @RequestMapping("/getStoragename")
    public List<String> getStoragename(HttpSession session){

        return outBoundManageService.getStorageName();
    }

    //确定订单是否符合添加要求
    @ResponseBody
    @RequestMapping("/checkAddOrder")
    public OutCountDelivery checkAddOrder(HttpServletRequest req){
        String orderid = req.getParameter("orderid");
        OutCountDelivery outCountDelivery = new OutCountDelivery();
        outCountDelivery.setOrderid(orderid);
        return outBoundManageService.getDeliveryByOrderId(outCountDelivery);
    }

    //开始添加出库信息
    @ResponseBody
    @RequestMapping("/addOutBound")
    public Integer addOutBound(HttpServletRequest req){
        String orderid = req.getParameter("orderid");
        String storagename = req.getParameter("storagename");
        String outboundtime = req.getParameter("outboundtime");
        String outboundman = req.getParameter("outboundman");
        OutBoundObject outBoundObject = new OutBoundObject();
        outBoundObject.setOrderid(orderid);
        outBoundObject.setStoragename(storagename);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date fianloutboundtime=null;
        try {
            fianloutboundtime = sdf.parse(outboundtime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        outBoundObject.setOutboundtime(fianloutboundtime);
        outBoundObject.setOutboundman(outboundman);
        return outBoundManageService.addOutBound(outBoundObject);
    }
}
